package com.home.homepage.service.postFooter.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eig.common.entity.AclPermission;
import com.eig.common.entity.homepageentity.postFooter.TbArticle;
import com.eig.common.entity.homepageentity.postFooter.TbBottomArticle;
import com.eig.common.vo.Result;
import com.home.homepage.dao.postFooter.ArticleDao;
import com.home.homepage.dao.postFooter.FooterDao;
import com.home.homepage.service.postFooter.FooterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author ：YQ袁（2446806940@qq.com）
 * @date ：Created in 2022/12/9 15:33
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class FooterServiceImpl implements FooterService {
    @Autowired
    private FooterDao footerDao;
    @Autowired
    private ArticleDao articleDao;

    @Override
    public Result<List<TbBottomArticle>> getAllBottomArticle(Integer current, Integer pageSize, TbBottomArticle tbBottomArticle) {
        IPage page = new Page(current,pageSize);
        List<TbBottomArticle> list1 = footerDao.selectList(null);
        List<TbArticle> tbArticles = articleDao.queryAll();
        List<TbBottomArticle> list = new ArrayList<>();
        QueryWrapper<TbBottomArticle> wrapper = new QueryWrapper<>();
        if (StringUtils.hasText(tbBottomArticle.getName())){
            wrapper.like("name",tbBottomArticle.getName());
        }
        if (StringUtils.hasText(tbBottomArticle.getIsDisable())){
            wrapper.eq("is_disable",tbBottomArticle.getIsDisable());
        }
         //遍历
        for (TbBottomArticle bottomArticle : list1) {
                if (bottomArticle.getParentId().equals("0")&& bottomArticle.getIsDeleted()==0){
                    list.add(bottomArticle);
                };
        }
        //遍历
        for (TbBottomArticle s : list) {
               s.setChildren(findChildren(list1, s.getId()));
        }
        //遍历
        for (TbBottomArticle bottomArticle : list) {
            //遍历
            for (TbArticle tbArticle : tbArticles) {
                if (bottomArticle.getId().contains(tbArticle.getIda())){
                    TbBottomArticle tbBottomArticle1 = new TbBottomArticle();
                    tbBottomArticle1.setName(tbArticle.getArtPic());
                    tbBottomArticle1.setId(tbArticle.getId());
                    bottomArticle.getChildren().add(tbBottomArticle1);
                }
            }
        }
        IPage iPage = footerDao.selectPage(page, wrapper);
        iPage.setRecords(list);
        Map<String,Object> map = new HashMap<>();
        map.put("records",list);
        map.put("iPage",iPage);
        return new Result(2000,"查询成功",map);
    }

    @Override
    public Result addBottomArticle(TbBottomArticle tbBottomArticle) {
        if (tbBottomArticle.getParentId().equals("0")){
            TbBottomArticle tbBottom = new TbBottomArticle();
            tbBottom.setName(tbBottomArticle.getName());
            tbBottom.setParentId("0");
            tbBottom.setIsDeleted(0);
            tbBottom.setIsDisable("0");
            tbBottom.setGmtCreate(new Date());
            tbBottom.setGmtModified(new Date());
            int insert = footerDao.insert(tbBottom);
            if (insert>0){
                return new Result(2000,"添加成功");
            }
        }
        if (tbBottomArticle.getParentId()!=null){
            TbBottomArticle tbBottom = new TbBottomArticle();
            tbBottom.setName(tbBottomArticle.getName());
            tbBottom.setParentId(tbBottomArticle.getParentId());
            tbBottom.setIsDeleted(0);
            tbBottom.setIsDisable("0");
            tbBottom.setGmtCreate(new Date());
            tbBottom.setGmtModified(new Date());
            int insert = footerDao.insert(tbBottom);
            if (insert>0){
                return new Result(2000,"修改成功");
            }
        }
        return new Result(5000,"失败");
    }

    @Override
    public Result changeStatus(String id, String isDisable) {
        UpdateWrapper<TbBottomArticle> wrapper = new UpdateWrapper<>();
        wrapper.eq("id ", id );
        wrapper.set("is_disable",isDisable);
        footerDao.update(null,wrapper);
        return new Result(2000,"修改成功");
    }

    @Override
    public Result deleteBottomArticle(TbBottomArticle tbBottomArticle) {
        if (tbBottomArticle.getId()!=null){
            UpdateWrapper<TbBottomArticle> wrapper = new UpdateWrapper<>();
            wrapper.eq("id ",tbBottomArticle.getId());
            wrapper.set("is_deleted" ,tbBottomArticle.getIsDeleted());
            int delete = footerDao.delete(wrapper);
            if (delete>0){
                return new Result(2000,"删除成功");
            }
        }
        return new Result(5000,"删除失败");
    }

    //3.方法的递归
    private List<TbBottomArticle> findChildren(List<TbBottomArticle> permissions, String id) {
        List<TbBottomArticle> seconds=new ArrayList<>();
        for(TbBottomArticle p:permissions){
            if(p.getParentId().equals(id)){
                seconds.add(p);
            }
        }
        for(TbBottomArticle p :seconds){
            p.setChildren(findChildren(permissions,p.getId()));
        }
        return seconds;
    }
}
